/* Copyright (C) 2013 Dr2co
 *
 * Created with IntelliJ IDEA.
 * User: Dr2co
 * Date: 01.07.13
 *
 *  pt-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  pt-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with pt-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.cron;

import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.cron.actions.CronItemBonus;
import com.aionengine.gameserver.model.templates.item.ItemTemplate;
import com.aionengine.gameserver.services.item.ItemInfoService;
import com.aionengine.gameserver.services.item.ItemService;
import com.aionengine.gameserver.world.World;
import com.aionengine.gameserver.world.knownlist.Visitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Dr2co
 */


public class ItemCronService {

    private static final Logger log = LoggerFactory.getLogger(ItemCronService.class);

    public static void start(final CronItemBonus bonus) {

        World.getInstance().doOnAllPlayers(new Visitor<Player>() {
            @Override
            public void visit(Player player) {
                ItemTemplate itemTemplate = ItemInfoService.getItemTemplate(bonus.getItemId());
                try {
                    if (player.getInventory().isFull(itemTemplate.getSpecialSlots())) {
                        log.warn("[OnlineBonus] player " + player.getName() + " tried to receive item with full inventory.");
                    } else {
                        ItemService.addItem(player, bonus.getItemId(), bonus.getCount());
                    }
                } catch (Exception ex) {
                    log.error("Exception during event rewarding of player " + player.getName(), ex);
                }
            }
        });
    }
}